require.config({
    paths: {
        Vue: "./libs/Vue.min-v2.6.12",
        AJAXPM: "./AJAXPM",
    },
    shim: {
        Vue: { exports: "Vue" },
        AJAXPM: { exports: 'AJAXPM' },

    }
});

require(
    ["Vue", "AJAXPM"],
    function(Vue, AJAXPM) {

        const vm = new Vue({
            el: "#app",
            data: {
                loginAdmin: {},
                userList: [],
                adminList: [],
            },
            methods: {
                userInfoEdit: () => {

                },
                userDel: UID => {

                },
                showUserInfo: UID => {

                },
                showAdminInfo: AID => {

                }
            },
            computed: {}
        });

        vm.$watch("", () => {

        });


        const doms = {
            usernameEl: document.getElementById("username"),
            logoutEl: document.getElementById("logout"),
            getUserListEl: document.getElementById("getUserList"),
            getAdminListEl: document.getElementById("getLiveList"),
            addEl: document.getElementById("add"),
            head1El: document.querySelector(".head1"),
            head2El: document.querySelector(".head2"),
            tabTitle1El: document.querySelector(".tabTitle1"),
            tabTitle2El: document.querySelector(".tabTitle2"),
            dataListEl: document.querySelector(".body > .list"),
        }
        const domRender = () => {
            let loginAdminStr = localStorage.getItem("loginAdmin");
            if (loginAdminStr) {
                let loginAdmin = JSON.parse(loginAdminStr);
                doms.usernameEl.innerHTML = `当前管理员: ${loginAdmin.username}`;
            } else {
                alert("用户检测失败");
            }
        };
        domRender();

        const domEvent = () => {
            // 登录
            doms.logoutEl.addEventListener("click", () => {
                document.cookie = "";
                sessionStorage.setItem("action", "logout");
                window.open("/", "_self");
            });

            // 获取用户列表
            doms.getUserListEl.addEventListener("click", () => {
                doms.dataListEl.innerHTML = "";
                AJAXPM.GET("/getUserList", [], result => {
                    let { code, msg, data } = JSON.parse(result);
                    switch (code) {
                        case "01":
                            let userList = data;
                            let listEl = document.querySelector(".body .list");
                            listEl.innerHTML = "";
                            userList.forEach(item => {
                                let li = `<li><span>${item.name}</span><span>${item.phonenum}</span><span>查看 删除 锁定</span>
                                    </li>`;
                                listEl.innerHTML += li;
                            });
                            doms.addEl.style.display = "block";

                            doms.head1El.style.display = "block";
                            doms.tabTitle1El.style.display = "block";

                            doms.head2El.style.display = "none";
                            doms.tabTitle2El.style.display = "none";
                            break;
                        default:
                            alert("获取列表失败！")
                    }
                });
            });

            doms.getAdminListEl.addEventListener("click", () => {
                doms.dataListEl.innerHTML = "";
                AJAXPM.GET("/getFruitsList", [], result => {
                    let { code, msg, data } = JSON.parse(result);
                    switch (code) {
                        case "01":
                            let userList = data;
                            let listEl = document.querySelector(".body .list");
                            listEl.innerHTML = "";
                            userList.forEach(item => {
                                let li =
                                    `<li>
                                    <span>${item.name}</span>
                                    <span>${item.price}</span>
                                    <span>${item.season}</span>
                                    <span>${item.storage}</span>
                                    <span>${item.pricetype}</span>
                                    <span>${item.con}</span>
                                    </li>`;
                                listEl.innerHTML += li;
                            });
                            doms.addEl.style.display = "block";

                            doms.head1El.style.display = "none";
                            doms.tabTitle1El.style.display = "none";

                            doms.head2El.style.display = "block";
                            doms.tabTitle2El.style.display = "block";
                            break;
                        default:
                            alert("获取列表失败！");
                    }
                });
            });

            doms.addEl.addEventListener("click", () => {
                document.querySelector(".add-box").style.display = "block";
            });

            document.querySelector(".add-commit").addEventListener("click", () => {
                let fname = document.getElementById("fname").value;
                let pricetype = document.getElementById("pricetype").value;
                let price = document.getElementById("price").value;
                let season = document.getElementById("season").value;
                let storage = document.getElementById("storage").value;
                let con = document.getElementById("con").value;

                if (!fname || !price || !pricetype || !season || !storage) return alert("信息不完整，无法添加！");

                let newF = { name: fname, pricetype, price, season, storage, con }

                AJAXPM.POST("/addFruits", newF, [], result => {
                    alert("添加成功");
                    console.log(result)
                    if (result.code === "01") {
                        alert("添加成功！");
                        this.getAdminListEl.click();
                    }
                });
                document.querySelector(".add-box").style.display = "none";
            })

            document.getElementById("seachKey").addEventListener("click", () => {
                let keyword = document.getElementById("keyword").value;
                AJAXPM.GET("/seachKey", ["keyword=" + keyword], result => {
                    let { msgCode, msgBody } = JSON.parse(result);
                    let userList = msgBody;
                    console.log(userList);
                    let listEl = document.querySelector(".body .list");
                    listEl.innerHTML = "";
                    userList.forEach(item => {
                        let li =
                            `<li><span>${item.username}</span><span>${item.location}</span>
                            <span>${item.buildtime}</span><span>${item.phonenum}</span><span>查看 删除 锁定</span>
                            </li>`;
                        listEl.innerHTML += li;
                    });
                });
            });
        }
        domEvent();
    }
);